﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProBugTracker.Core.Interface;
using ProBugTracker.Domain;
using ProBugTracker.Data.Interface;
using ProBugTracker.Data;

namespace ProBugTracker.Core
{
    public class PermissionManager : IPermissionManager
    {
        private IPermissionManagerData _permissionManagerData;

        public PermissionManager(IPermissionManagerData permissionManagerData)
        {
            this._permissionManagerData = permissionManagerData;
        }

        public bool AddNewPermission(string userPermissionName)
        {
            try
            {
                RolePermissionDB roleAssignPermissionDB = new RolePermissionDB()
                {
                    PermissionName=userPermissionName
                };
                return _permissionManagerData.AddNewPermission(roleAssignPermissionDB);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public RoleAssignPermissionDB AddRolePermission(RoleAssignPermission rolePermission)
        {
            try
            {
                RoleAssignPermissionDB roleAssignPermissionDB = new RoleAssignPermissionDB()
                {
                    UserRoleID=rolePermission.UserRoleID,
                    UserPermissionID=rolePermission.UserRolePermissionID,
                    PermissionStartDate=rolePermission.PermissionStartDate,
                    PermissionEndDate=rolePermission.PermissionEndDate,
                    Active=true
                };

                return _permissionManagerData.AddRolePermission(roleAssignPermissionDB);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool RemoveRolePermission(int roleID, int permissionID)
        {
            try
            {
                return _permissionManagerData.RemoveRolePermission(roleID, permissionID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public IList<RoleAssignPermission> ReadRolePermissionByRoleID(int roleID)
        {
            try
            {
                IList<RoleAssignPermissionDB> roleAssignPermissionDBList = _permissionManagerData.ReadRolePermissionByRoleID(roleID);
                IList<RoleAssignPermission> roleAssignPermissionList = new List<RoleAssignPermission>();

                foreach (RoleAssignPermissionDB roleAssignPermissionDB in roleAssignPermissionDBList)
                {
                    string permissionName = _permissionManagerData.ReadPermissionByPermissionID(roleAssignPermissionDB.UserPermissionID).PermissionName;
                    RoleAssignPermission roleAssignPermission = new RoleAssignPermission()
                    {
                        UserRoleID = roleAssignPermissionDB.UserRoleID,
                        UserRolePermissionID = roleAssignPermissionDB.UserPermissionID,
                        PermissionName = permissionName,
                        PermissionStartDate = roleAssignPermissionDB.PermissionStartDate,
                        PermissionEndDate=roleAssignPermissionDB.PermissionEndDate,
                        Active=roleAssignPermissionDB.Active
                    };
                    roleAssignPermissionList.Add(roleAssignPermission);
                }
                return roleAssignPermissionList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
